import { reqUserLogin ,reqUserLoginOut} from "@/api"

import { Message } from "element-ui"
const state={


userInfo:{
  name:localStorage.getItem('name'),
  token:localStorage.getItem('token')
}


}
const actions={
//实现用户登录
async  userLogin({commit},obj){
  const res=await reqUserLogin(obj)
  if(res.code==200){
    commit('SAVE_USER_INFO',res.data)
    //将其中的信息存入到localstorage
    localStorage.setItem('name',res.data.name)
    localStorage.setItem('token',res.data.token)
    Message.success("用户登录成功")
  }else{
    Message.error('用户登录失败')
   return Promise.reject(res.message)
  
    
  }

},
async userLoginOut({commit}){
  const res=await reqUserLoginOut()
  if(res.code==200){
    //  清楚本地缓存
    localStorage.removeItem('name'),
    localStorage.removeItem('token'),
    //清楚本地vuex的数据
    commit('Remove_userInfo')
  }else{
    Promise.reject(res.message)
  }
}



}
const mutations={
  //存储数据

  SAVE_USER_INFO(state,userInfo){
    state.userInfo=userInfo;
  },
  //清空数据
  Remove_userInfo(state){
    state.userInfo={
      name:'',
      token:'',
    }
  }

}
const getters={}
export default {
namespaced:true,
state,
actions,
mutations,
getters
}